layui.use(['table', 'admin', 'ax', 'func', 'form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var form = layui.form;
    var func = layui.func;

    /**
     * 生产工艺管理
     */
    var TProductionProcess = {
        tableId: "tProductionProcessTable"
    };

    /**
     * 初始化表格的列
     */
    TProductionProcess.initColumn = function () {
        return [[
            {field: 'companyName', sort: true, title: '所属企业'},
            {field: 'processDec', sort: true, title: '生产工艺简介'},
            // {field: 'businessLicense',  align: 'center', title: '营业执照',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.businessLicense+'") ><img id="img" src="'+d.businessLicense+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            // {field: 'eiaApproval', align: 'center', title: '环评批复',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.eiaApproval+'") ><img id="img" src="'+d.eiaApproval+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            // {field: 'eiaAcceptance',align: 'center', title: '环评验收',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.eiaAcceptance+'") ><img id="img" src="'+d.eiaAcceptance+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            // {field: 'pollutionPermit', align: 'center', title: '排污许可证',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.pollutionPermit+'") ><img id="img" src="'+d.pollutionPermit+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            // {field: 'floorPlan',align: 'center', title: '厂区平面图',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.floorPlan+'") ><img id="img" src="'+d.floorPlan+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            // {field: 'msds', align: 'center', title: '涉VOCs原辅料MSDS',templet: function (d) {
            //         return '<div onclick=showMedia("img","image","'+d.msds+'") ><img id="img" src="'+d.msds+'" alt="" width="100px" height="50px"></ a></div>';
            //     }},
            {field: 'flowChart', align: 'center', title: '生产工艺流程图',templet: function (d) {
                    return '<div onclick=showMedia("img","image","'+d.flowChart+'") ><img id="img" src="'+d.flowChart+'" alt="" width="100px" height="50px"></ a></div>';
                }},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };








    /**
     * 点击查询按钮
     */
    TProductionProcess.search = function () {
        var queryData = {};
        queryData['companyName'] = $("#companyName").val();
        table.reload(TProductionProcess.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    TProductionProcess.openAddDlg = function () {
        func.open({
            title: '添加生产工艺',
            content: Feng.ctxPath + '/tProductionProcess/add',
            tableId: TProductionProcess.tableId
        });
    };

    /**
    * 点击编辑
    *
    * @param data 点击按钮时候的行数据
    */
    TProductionProcess.openEditDlg = function (data) {
        func.open({
            title: '修改生产工艺',
            content: Feng.ctxPath + '/tProductionProcess/edit?id=' + data.id,
            tableId: TProductionProcess.tableId
        });
    };

    /**
     * 导出excel按钮
     */
    TProductionProcess.exportExcel = function () {
        var checkRows = table.checkStatus(TProductionProcess.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    TProductionProcess.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/tProductionProcess/delete", function (data) {
                if (data.code == 200) {
                    Feng.success("删除成功!");
                }else{
                    Feng.error("删除失败!" + data.message + "!");
                }
                table.reload(TProductionProcess.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + TProductionProcess.tableId,
        url: Feng.ctxPath + '/tProductionProcess/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: TProductionProcess.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        TProductionProcess.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        TProductionProcess.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        TProductionProcess.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + TProductionProcess.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            TProductionProcess.openEditDlg(data);
        } else if (layEvent === 'delete') {
            TProductionProcess.onDeleteItem(data);
        }
    });

    form.on('switch(delFlag)', function (obj) {
        var id = obj.elem.value;
        var checked = obj.elem.checked ? true : false;

         TProductionProcess.changeDelFlag(id, checked);
    });

    /**
     * 修改状态
     *
     * @param userId 用户id
     * @param checked 是否选中（true,false），选中就是解锁用户，未选中就是锁定用户
     */
     TProductionProcess.changeDelFlag = function (id, checked) {
        if (checked) {
            var ajax = new $ax(Feng.ctxPath + "/tProductionProcess/enable", function (data) {
                if (data.code == 200) {
                    Feng.success("启用成功!");
                }else{
                    Feng.error("启用失败!" + data.message + "!");
                }
                table.reload( TProductionProcess.tableId);
            }, function (data) {
                Feng.error("启用失败!" + data.message + "!");
                table.reload( TProductionProcess.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        } else {
            var ajax = new $ax(Feng.ctxPath + "/tProductionProcess/disable", function (data) {
                if(data.code == 200) {
                    Feng.success("禁用成功!");
                }else{
                    Feng.error("禁用失败!" + data.message + "!");
                }
                table.reload( TProductionProcess.tableId);
            }, function (data) {
                Feng.error("禁用失败!" + data.message + "!");
                table.reload( TProductionProcess.tableId);
            });
            ajax.set("id", id);
            ajax.start();
        }
    };

});

function showMedia(id, type, src) {
    var $ = layui.jquery;
    var idBar ="#"+id;
    var width = $(idBar).width();
    var height = $(idBar).height();
    var scaleWH = width / height;
    var bigH = 500;
    var bigW = scaleWH * bigH;
    if (bigW > 900) {
        bigW = 900;
        bigH = bigW / scaleWH;
    } // 放大预览图片
    layer.open({
        type: 1,
        title: false,
        closeBtn: 1,
        shadeClose: true,
        area: [bigW + 'px', bigH + 'px'], //宽高
        content: "<img width='" + bigW + "' height='" + bigH + "'controls=\"controls\" src=" + src + " />"
    });
}
